<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="合同名称" prop="contractName">
      <el-input v-model="dataForm.contractName" placeholder="合同名称"></el-input>
    </el-form-item>
    <el-form-item label="合同项目" prop="contractItem">
      <el-input v-model="dataForm.contractItem" placeholder="合同项目"></el-input>
    </el-form-item>
    <el-form-item label="合同发起时间" prop="initiateTime">
      <el-input v-model="dataForm.initiateTime" placeholder="合同发起时间"></el-input>
    </el-form-item>
    <el-form-item label="签单时间" prop="signTime">
      <el-input v-model="dataForm.signTime" placeholder="签单时间"></el-input>
    </el-form-item>
    <el-form-item label="合同编号" prop="contractNo">
      <el-input v-model="dataForm.contractNo" placeholder="合同编号"></el-input>
    </el-form-item>
    <el-form-item label="合同状态，0-未生效，1-已生效，2-已过期" prop="status">
      <el-input v-model="dataForm.status" placeholder="合同状态，0-未生效，1-已生效，2-已过期"></el-input>
    </el-form-item>
    <el-form-item label="订单编号" prop="orderNo">
      <el-input v-model="dataForm.orderNo" placeholder="订单编号"></el-input>
    </el-form-item>
    <el-form-item label="商品编号" prop="productNo">
      <el-input v-model="dataForm.productNo" placeholder="商品编号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          contractName: '',
          contractItem: '',
          initiateTime: '',
          signTime: '',
          contractNo: '',
          status: '',
          orderNo: '',
          productNo: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          contractName: [
            { required: true, message: '合同名称不能为空', trigger: 'blur' }
          ],
          contractItem: [
            { required: true, message: '合同项目不能为空', trigger: 'blur' }
          ],
          initiateTime: [
            { required: true, message: '合同发起时间不能为空', trigger: 'blur' }
          ],
          signTime: [
            { required: true, message: '签单时间不能为空', trigger: 'blur' }
          ],
          contractNo: [
            { required: true, message: '合同编号不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '合同状态，0-未生效，1-已生效，2-已过期不能为空', trigger: 'blur' }
          ],
          orderNo: [
            { required: true, message: '订单编号不能为空', trigger: 'blur' }
          ],
          productNo: [
            { required: true, message: '商品编号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/core/contract/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.contract.userId
                this.dataForm.contractName = data.contract.contractName
                this.dataForm.contractItem = data.contract.contractItem
                this.dataForm.initiateTime = data.contract.initiateTime
                this.dataForm.signTime = data.contract.signTime
                this.dataForm.contractNo = data.contract.contractNo
                this.dataForm.status = data.contract.status
                this.dataForm.orderNo = data.contract.orderNo
                this.dataForm.productNo = data.contract.productNo
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/core/contract/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'contractName': this.dataForm.contractName,
                'contractItem': this.dataForm.contractItem,
                'initiateTime': this.dataForm.initiateTime,
                'signTime': this.dataForm.signTime,
                'contractNo': this.dataForm.contractNo,
                'status': this.dataForm.status,
                'orderNo': this.dataForm.orderNo,
                'productNo': this.dataForm.productNo
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
